
package com.founder.bm.util;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.founder.bm.entity.EnrollDtlTo;
import com.founder.bm.entity.EnrollInfTo;
import com.founder.bm.entity.LoginUserTo;
import com.founder.bm.util.FileOperate;

public final class ExcelUtil {

	private ExcelUtil() {

	}

	private static void createCell(HSSFCellStyle style1, HSSFRow row,
		int j, int age) {
		HSSFCell avgNum1 = row.createCell(j);
		avgNum1.setCellValue(age);
		avgNum1.setCellStyle(style1);
	}
	
	/**
	 * 保存excel并返回保存路径
	 * 
	 * @param wb
	 *                excel
	 * @param loginId
	 *                登陆者ID
	 * @return 保存路径
	 */
	public static String saveGeneratedExcel(HSSFWorkbook wb, Long loginId, String fileTitle) {
		java.util.Date date = new java.util.Date();
		String path =
			"\\excel\\" + fileTitle + "_" + loginId
			+ date.getTime() + ".xls";
		String fullPath = FileOperate.getDir("saveDir") + path;
		File file = new File(fullPath);
		try {
			if (file != null && !file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(file);
			wb.write(fos);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}
	
	public static String saveGeneratedTXT(List<LoginUserTo> dataList, Long loginId, String fileTitle){
		java.util.Date date = new java.util.Date();
		String path =
			"\\excel\\" + fileTitle + "_" + loginId
			+ date.getTime() + ".txt";
		String fullPath = FileOperate.getDir("saveDir") + path;
		File file = new File(fullPath);
		
		StringBuilder sb=new StringBuilder("");
		if (null != dataList && !dataList.isEmpty()) {			
			for(LoginUserTo user : dataList){
				sb.append(user.getUserName());
				sb.append("\r\n");
				EnrollInfTo inf = user.getEnrollInfo();
				if(null != inf){
					sb.append("领队：");
					sb.append(inf.getLeaderName());
					sb.append("(");
					sb.append(inf.getLeaderTel());
					sb.append(")	");
					
					sb.append("教练：");
					sb.append(inf.getMasterName());
					sb.append("(");
					sb.append(inf.getMasterTel());
					sb.append(")");
					
					sb.append("\r\n");
					sb.append("选手：");
					sb.append(inf.getPlayers());
				}
				sb.append("\r\n");
				sb.append("\r\n");
			}
		}
		
		try {
			if (file != null && !file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(file);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
			bw.write(sb.toString());
            bw.flush();
            bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}

	public static HSSFWorkbook baoMingDataExcel(List<LoginUserTo> dataList){
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("Sheet1");
		int rowNum = 0;
		int cellNum = 0;
		
		// 设置居中式样
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 设置字体--黑体
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setFont(font);
		//titleCell.setCellStyle(style);
		HSSFCellStyle styleBold = wb.createCellStyle();
		styleBold.setFont(font);			

		HSSFCellStyle dateStyle = wb.createCellStyle();
		HSSFDataFormat format = wb.createDataFormat();
		dateStyle.setDataFormat(format.getFormat("dd-MM-yyyy"));
		
		HSSFRow topRow1 = sheet.createRow(rowNum++);

		String[] arr = {"组别", "学校", "班级", "姓名", "项目"};
		for (int i = 0; i < arr.length; i++) {
			HSSFCell temp = topRow1.createCell(i);
			temp.setCellValue(arr[i].toString());
		}
		
		if (null != dataList && !dataList.isEmpty()) {			
			for(LoginUserTo user : dataList){
				for(EnrollDtlTo dtl : user.getEnrollDtlList()){
					HSSFRow row = sheet.createRow(rowNum++);
					int j = 0;
					HSSFCell avgNum0 = row.createCell(j++);
					avgNum0.setCellValue(dtl.getZuBie());
					
					HSSFCell avgNum1 = row.createCell(j++);
					avgNum1.setCellValue(user.getUserName());
					
					HSSFCell avgNum2 = row.createCell(j++);
					avgNum2.setCellValue(dtl.getBanji());
					
					HSSFCell avgNum3 = row.createCell(j++);
					avgNum3.setCellValue(dtl.getPlayerName());
					
					HSSFCell avgNum4 = row.createCell(j++);
					avgNum4.setCellValue(dtl.getItem());
				}
				
			}
		}
		return wb;
	}
}
